import json
import datetime

from flask import Blueprint
from flask import request
from flask import Response

from sqlalchemy import Column, String, Integer
from flask_sqlalchemy import SQLAlchemy

from app.comm.db import db
from app.system import auth
from app.comm.db import jsonformater
from app.comm.pub import RestEntity
from app.comm.pub import class2dict

appmenuservice = Blueprint('appmenu', __name__, )


class DataResource(db.Model):
    __tablename__ = 'app_menu'
    res_id = Column(Integer, primary_key=True)
    res_name = Column(String(80), unique=True, nullable=False)
    author = Column(String(80), unique=True, nullable=False)
    max_type = Column(String(80), unique=True, nullable=False)
    mid_type = Column(String(80), unique=True, nullable=False)
    min_type = Column(String(80), unique=True, nullable=False)
    order_num = Column(String(80), unique=True, nullable=False)
    icon = Column(String(80), unique=True, nullable=False)
    url = Column(String(80), unique=True, nullable=False)
    memo = Column(String(80), unique=True, nullable=False)
    visible = Column(String(80), unique=True, nullable=False)
    create_by = Column(String(80), unique=True, nullable=False)
    create_time = Column(String(80), unique=True, nullable=False)
    update_by = Column(String(80), unique=True, nullable=False)
    update_time = Column(String(80), unique=True, nullable=False)
    remark = Column(String(80), unique=True, nullable=False)

    def as_dict(self):
        return {c.name: str(getattr(self, c.name)) for c in self.__table__.columns}

    def __repr__(self):
        return '<User %r>' % self.res_id


@appmenuservice.route("/rest/app/menu/list", methods=['POST', 'GET'])
def getAppMenuList():
    appid = request.args.get('appid')

    # dict1 = json.loads(a)
    tables = db.session.execute(
        "SELECT a.app_id,a.order_num,m.* from app_menu_app a LEFT JOIN app_menu m on a.menu_id=m.menu_id where a.app_id='"+appid+"' and m.visible=1").fetchall()
    datas = [dict(r) for r in tables]
    result = RestEntity(0, 0, datas, "获取数据！")
    return json.dumps(class2dict(result), default=jsonformater)

    # return json.dumps(dict1)


'''
用户是否购买了当前app的资源
'''


@appmenuservice.route("/rest/app/buy/getbyappid", methods=['POST', 'GET'])
def getAppMenuByAppId():
    appid = request.args.get('appid')

    # dict1 = json.loads(a)
    tables = db.session.execute(
        "SELECT a.app_id,a.order_num,m.* from app_menu_app a LEFT JOIN app_menu m on a.menu_id=m.menu_id where a.app_id='"+appid+"' and m.visible=1").fetchall()
    datas = [dict(r) for r in tables]
    result = RestEntity(0, 0, datas, "获取数据！")
    return json.dumps(class2dict(result), default=jsonformater)

    # return json.dumps(dict1)


'''
用户是否购买了当前app的资源
'''


@appmenuservice.route("/rest/app/buy/buyapp", methods=['POST', 'GET'])
def buyApp():
    appid = request.args.get('appid')

    # dict1 = json.loads(a)
    tables = db.session.execute(
        "").fetchall()
    datas = [dict(r) for r in tables]
    result = RestEntity(0, 0, datas, "获取数据！")
    return json.dumps(class2dict(result), default=jsonformater)

    # return json.dumps(dict1)


'''
检查用户是否购买了当前app的资源
'''


@appmenuservice.route("/rest/app/buy/checkbyappid", methods=['POST', 'GET'])
def checkByAppId():
    appid = request.args.get('appid')
    menuid = request.args.get('appid')
    
    payload = auth.decode_auth_token(request)
    userid = payload.data.userid

    # dict1 = json.loads(a)
    tables = db.session.execute(
        "SELECT am.* from app_menu am LEFT JOIN app_menu_app ama on am.menu_id=ama.menu_id LEFT JOIN sys_user_app aua on ama.app_id=aua.app_id where aua.user_id="+userid+" and aua.app_id='"+appid+"' and am.menu_id="+menuid+" and am.visible=1").fetchall()
    datas = [dict(r) for r in tables]
    result = RestEntity(0, 0, datas, "获取数据！")
    return json.dumps(class2dict(result), default=jsonformater)

    # return json.dumps(dict1)
